package com.youlu.campus.admin.sesamekefu;

import com.youlu.campus.common.utils.AppContext;
import com.youlu.campus.entity.UniversityInfo;
import com.youlu.campus.entity.VO.req.OrderRefundOptVO;
import com.youlu.campus.entity.VO.req.UniversityInfoQueryVO;
import com.youlu.campus.entity.sesamekefu.SesameKeFuQuery;
import com.youlu.campus.service.notify.KuaiDi100Service;
import com.youlu.campus.service.order.OrderService;
import com.youlu.campus.service.sesamekefu.SesameKeFuService;
import com.youlu.campus.service.university.UniversityInfoService;
import com.yuelin.infrastructure.quantum.common.QResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@Slf4j
@RestController
public class SesameKeFuController {

    @Autowired
    private SesameKeFuService sesameKeFuService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private UniversityInfoService universityInfoService;

    @Autowired
    private KuaiDi100Service kuaiDi100Service;

    @PostMapping("/sesamekefu/order/list")
    public QResult list(@RequestBody SesameKeFuQuery sesameKeFuQuery){
        return QResult.success(sesameKeFuService.userOrderList(sesameKeFuQuery));
    }

    @PostMapping("/sesamekefu/order/address/update")
    public QResult addressUpdate(@RequestBody com.youlu.campus.entity.domain.OrderUpdateRequest request) {
        String name = AppContext.current().getName();
        log.info("当前登录人 ->{}",name);
        request.setOptUser(name);
        request.setSourcePlatform("芝麻客服");
        orderService.addressUpdate(request);
        return QResult.success(null);
    }

    @GetMapping("/sesamekefu/order/find-one")
    public QResult findOne(@RequestParam("id") String id) {
        return QResult.success(orderService.findOne(id));
    }


    @PostMapping("/sesamekefu/university/list")
    public QResult list(@NotNull @RequestBody UniversityInfoQueryVO request) {
        Page<UniversityInfo> datas = universityInfoService.list(request);
        return QResult.success(datas);
    }

    @PostMapping("/sesamekefu/order/pre-refund")
    public QResult preRefund(@RequestBody OrderRefundOptVO req) {

        req.setRefundApplyUserType("platform");
        String name = AppContext.current().getName();
        req.setOptName(name);
        boolean res = orderService.preRefund(req);
        if (res) {
            return QResult.success(null);
        }
        return QResult.fail("退款错误");
    }

    @PostMapping("/sesamekefu/order/do-refund")
    public QResult doRefund(@RequestBody OrderRefundOptVO req) {
        String name = AppContext.current().getName();
        req.setOptName(name);
        req.setSourcePlatform("芝麻客服");
        boolean res = orderService.doRefund(req);
        if (res) {
            return QResult.success(null);
        }
        return QResult.fail("退款错误");
    }

    @GetMapping("/sesamekefu/order/track")
    public QResult subscribe(@RequestParam String orderNo) {
        return QResult.success(kuaiDi100Service.queryTrack(orderNo));
    }






}
